package com.yidu.best.TA.Fundinventory.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yidu.best.TA.Fundinventory.dao.FundinventoryDao;
import com.yidu.best.TA.Fundinventory.domain.Casharap;
import com.yidu.best.TA.Fundinventory.domain.CasharapInventory;
import com.yidu.best.TA.Fundinventory.domain.Fundinventory;
import com.yidu.best.TA.Fundinventory.domain.FundinventoryVo;
import com.yidu.best.TA.Fundinventory.service.FundinventoryService;
import com.yidu.best.TA.Ta.util.IDUtil;
import com.yidu.best.basicSetup.accountset.domain.Account;
import com.yidu.best.basicSetup.fundset.domain.Fund;
import com.yidu.best.bond.cashInventory.domain.CashInventory;

@Service
public class FundinventoryImpl implements FundinventoryService {
	@Autowired
	private FundinventoryDao fundinventoryDao;

	@Override
	public Map<String, Object> findFundinventory(FundinventoryVo fundinventoryVo) {
		Map<String, Object> map = new HashMap<String, Object>();
		int count = fundinventoryDao.findCountFundinventory(fundinventoryVo);
		int page = (fundinventoryVo.getPage() - 1) * fundinventoryVo.getLimit();
		fundinventoryVo.setPage(page);
		List<Fundinventory> findFundinventory = fundinventoryDao.findFundinventory(fundinventoryVo);
		map.put("count", count);
		map.put("data", findFundinventory);
		map.put("msg", "");
		map.put("code", 0);
		return map;
	}

	@Override
	public Map<String, Object> find(String fundinventory) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Fundinventory> find = fundinventoryDao.find(fundinventory);
		map.put("data", find);
		map.put("msg", "");
		map.put("code", 0);
		return map;
	}

	@Override
	public int insertCashinventory(Fundinventory fundinventory) {
		int result = 0;
		Casharap casharap1 = new Casharap();
		Account Account1 = fundinventoryDao.selectAccount(fundinventory.getFundId());
		Fund fund1 = fundinventoryDao.selectFund(fundinventory.getFundId());
		casharap1.setCashARAPId(IDUtil.getUUID());
		casharap1.setCashARAPNo(
				"XJARAP-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "-"
						+ IDUtil.getUUIDA());
		casharap1.setFundId(fundinventory.getFundId());
		casharap1.setFundCode(fundinventory.getFundCode());
		casharap1.setFundName(fundinventory.getFundName());
		casharap1.setAccountId(Account1.getAccountId());
		casharap1.setAccountNo(Account1.getAccountNO());
		casharap1.setAccountName(Account1.getAccountName());
		Integer nav1 = Integer.valueOf(fundinventory.getNav().intValue());
		Integer share1 = fundinventory.getShare();
		Integer zong1 = nav1 += share1;
		Integer num1 = zong1 * Integer.valueOf(fund1.getHostingFees().intValue()) / 360;
		casharap1.setArapAmount(BigDecimal.valueOf(num1));
		casharap1.setArapDate(new Date());
		casharap1.setArapFlag(2);
		casharap1.setArapType(4);
		casharap1.setDescription("托管费");

		Casharap casharap2 = new Casharap();
		Account Account2 = fundinventoryDao.selectAccount(fundinventory.getFundId());
		Fund fund2 = fundinventoryDao.selectFund(fundinventory.getFundId());
		casharap2.setCashARAPId(IDUtil.getUUID());
		casharap2.setCashARAPNo(
				"XJARAP-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "-"
						+ IDUtil.getUUIDA());
		casharap2.setFundId(fundinventory.getFundId());
		casharap2.setFundCode(fundinventory.getFundCode());
		casharap2.setFundName(fundinventory.getFundName());
		casharap2.setAccountId(Account2.getAccountId());
		casharap2.setAccountNo(Account2.getAccountNO());
		casharap2.setAccountName(Account2.getAccountName());
		Integer nav2 = Integer.valueOf(fundinventory.getNav().intValue());
		Integer share2 = fundinventory.getShare();
		Integer zong2 = nav2 += share2;
		Integer num2 = zong2 * Integer.valueOf(fund2.getManagementFee().intValue()) / 360;
		casharap2.setArapAmount(BigDecimal.valueOf(num2));
		casharap2.setArapDate(new Date());
		casharap2.setArapFlag(2);
		casharap2.setArapType(3);
		casharap2.setDescription("管理费");

		result = fundinventoryDao.insertCashinventory(casharap1);
		result = fundinventoryDao.insertCashinventory(casharap2);
		return result;
	}

	@Override
	public int insertCashinventoryinventory(Fundinventory fundinventory) {
		int result = 0;
		List<Casharap> list = fundinventoryDao.selectcasharap(fundinventory.getFundId());
		for (Casharap casharap : list) {
			Fund fund = fundinventoryDao.selectFund(casharap.getFundId());
			Account account = fundinventoryDao.selectAccount(casharap.getFundId());
			int i = fundinventoryDao.selectCountcashARAPinventory(casharap.getFundId(), casharap.getArapType());
			if (i == 0) {
				CasharapInventory casharapInventory = new CasharapInventory();
				casharapInventory.setCachARAPInventoryId(IDUtil.getUUID());
				casharapInventory.setCachARAPInventoryNo(
						"XJYSYFKC-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
								+ "-" + IDUtil.getUUIDA());
				casharapInventory.setFundId(fund.getFundId());
				casharapInventory.setFundCode(fund.getFundNo());
				casharapInventory.setFundName(fund.getFundName());
				casharapInventory.setAccountId(account.getAccountId());
				casharapInventory.setAccountNo(account.getAccountNO());
				casharapInventory.setAccountName(account.getAccountName());
				if (casharap.getArapType() == 3) {
					casharapInventory.setBalance(casharap.getArapAmount());
				} else {
					casharapInventory.setBalance(casharap.getArapAmount());
				}
				casharapInventory.setBusinessDate(new Date());
				if (casharap.getArapType() == 3) {
					casharapInventory.setBusinessType(casharap.getArapType());
				} else {
					casharapInventory.setBusinessType(casharap.getArapType());
				}
				casharapInventory.setFlag(-1);
				if (casharap.getArapType() == 3) {
					casharapInventory.setDescription("管理费");
				} else {
					casharapInventory.setDescription("托管费");
				}
				result = fundinventoryDao.insertCashinventoryinventory(casharapInventory);
			} else {
				CasharapInventory casharapInventory = new CasharapInventory();
				casharapInventory.setBusinessType(casharap.getArapType());
				if (casharap.getArapType() == 3) {
					casharapInventory.setBalance(casharap.getArapAmount().add(casharap.getArapAmount()));
				} else {
					casharapInventory.setBalance(casharap.getArapAmount().add(casharap.getArapAmount()));
				}
				result = fundinventoryDao.updateCashinventoryinventory(casharapInventory);
			}
		}
		return result;
	}

	@Override
	public int addcasharap(Fundinventory fundinventory) {
		int result = 0;
		Casharap casharap = new Casharap();
		Fund fund = fundinventoryDao.selectFund(fundinventory.getFundId());
		Account account = fundinventoryDao.selectAccount(fund.getAccountId());
		CashInventory cashInventory = fundinventoryDao.findcashinventory(fundinventory.getFundId());
		casharap.setCashARAPId(IDUtil.getUUID());
		casharap.setCashARAPNo(
				"XJARAP-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "-"
						+ IDUtil.getUUIDA());
		casharap.setFundId(fund.getFundId());
		casharap.setFundCode(fund.getFundNo());
		casharap.setFundName(fund.getFundName());
		casharap.setAccountId(account.getAccountId());
		casharap.setAccountNo(account.getAccountNO());
		casharap.setAccountName(account.getAccountName());
		BigDecimal num = new BigDecimal(0.15);
		casharap.setArapAmount(cashInventory.getCashBalance().multiply(num));
		casharap.setArapDate(new Date());
		casharap.setArapFlag(1);
		casharap.setArapType(2);
		result = fundinventoryDao.addcasharap(casharap);
		return result;
	}

	@Override
	public int addcasharapinventory(Fundinventory fundinventory) {
		int result = 0;
		Fund fund = fundinventoryDao.selectFund(fundinventory.getFundId());
		Account account = fundinventoryDao.selectAccount(fund.getFundId());
		int i =	fundinventoryDao.findCasharapinventoryto(2);
		CashInventory cashInventory = fundinventoryDao.findcashinventory(fundinventory.getFundId());
		if (i == 0) {
			CasharapInventory casharapInventory = new CasharapInventory();
			casharapInventory.setCachARAPInventoryId(IDUtil.getUUID());
			casharapInventory.setCachARAPInventoryNo(
					"XJYSYFKC-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "-"
							+ IDUtil.getUUIDA());
			casharapInventory.setFundId(fund.getFundId());
			casharapInventory.setFundCode(fund.getFundNo());
			casharapInventory.setFundName(fund.getFundName());
			casharapInventory.setAccountId(account.getAccountId());
			casharapInventory.setAccountNo(account.getAccountNO());
			casharapInventory.setAccountName(account.getAccountName());
			BigDecimal num = new BigDecimal(0.15);
			casharapInventory.setBalance(cashInventory.getCashBalance().multiply(num));
			casharapInventory.setBusinessDate(new Date());
			casharapInventory.setBusinessType(2);
			casharapInventory.setFlag(1);
			casharapInventory.setDescription("现金计息");
			result = fundinventoryDao.addcasharapinventory(casharapInventory);
		} else {
			CasharapInventory casharapInventory = new CasharapInventory();
			CasharapInventory inventory = null;
			List<Casharap> list2 = fundinventoryDao.selectcasharap(fund.getFundId());
			for (Casharap casharap : list2) {
				inventory= fundinventoryDao.findCasharapinventory(fund.getFundId(),casharap.getArapType());
			}
			BigDecimal num = new BigDecimal(0.15);
			casharapInventory.setBalance(cashInventory.getCashBalance().multiply(num).add(inventory.getBalance()));
			casharapInventory.setFundId(fund.getFundId());
			result = fundinventoryDao.upCasharapinventory(casharapInventory);
		}
		return result;
	}
}
